package com.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.dao.IBankMetaDao;
import com.dao.entity.Apply;
import com.dao.entity.BankMeta;
import com.service.IBankMetaService;
import com.utils.commons.PageUtils;

@Service
public class BankMetaServiceImpl implements IBankMetaService{
	
	@Autowired
	private IBankMetaDao bankMetaDao;

	@Override
	public Page<BankMeta> applySupport(Apply apply, Integer page, Integer size) {
		Pageable pageable = PageUtils.toPageable(page, size);
		Map<String, Object> values = new HashMap<String, Object>();
		StringBuffer queryString = new StringBuffer("SELECT dc.id as id, dc.bankName as bankName, dc.landType as landType, dc.loanLimit as loanLimit, ")
				.append("dc.house_loan_m as house_loan_m, dc.age_loan_m as age_loan_m, ")
				.append("dc.house_loan_fe as house_loan_fe, dc.age_loan_fe as age_loan_fe, ")
				.append("dc.houseAge as houseAge, dc.createdate as createdate, dc.updatedate as updatedate, dc.isdelete as isdelete\r\n")
				.append("FROM loan_bankmeta dc\r\n")
				.append("where ");
		values.put("landType", "%"+apply.getLandType()+"%");
		queryString.append(" dc.landType like :landType\r\n");
		values.put("loanLimit", apply.getLoanAge());
		queryString.append(" and dc.loanLimit >= :loanLimit\r\n");
		values.put("houseAge", apply.getHouseAge());
		queryString.append(" and dc.houseAge >= :houseAge\r\n");
		if(apply.getSex() == 1) {
			values.put("house_loan_m", apply.getHouseAge()+apply.getLoanAge());
			queryString.append(" and dc.house_loan_m >= :house_loan_m\r\n");
			values.put("age_loan_m", apply.getLoanAge()+apply.getAge());
			queryString.append(" and dc.age_loan_m >= :age_loan_m\r\n");
		}else {
			values.put("house_loan_fe", apply.getHouseAge()+apply.getLoanAge());
			queryString.append(" and dc.house_loan_fe >= :house_loan_fe\r\n");
			values.put("age_loan_fe", apply.getLoanAge()+apply.getAge());
			queryString.append(" and dc.age_loan_fe >= :age_loan_fe\r\n");
		}
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return bankMetaDao.sqlQuery(queryString.toString(), countString.toString(), values, pageable);
	}

	@Override
	public BankMeta findById(Long bankId) {
		return bankMetaDao.findById(bankId).orElse(new BankMeta());
	}

	@Override
	public BankMeta saveBankMeta(BankMeta bankMeta) {
		return bankMetaDao.save(bankMeta);
	}
	
	@Override
	public Page<BankMeta> findByCondition(BankMeta bankMeta, Integer page, Integer size) {
		Pageable pageable = PageUtils.toPageable(page, size);
		Map<String, Object> values = new HashMap<String, Object>();
		StringBuffer queryString = new StringBuffer("SELECT dc.id as id, dc.bankName as bankName, dc.landType as landType, dc.loanLimit as loanLimit, ")
				.append("dc.house_loan_m as house_loan_m, dc.age_loan_m as age_loan_m, ")
				.append("dc.house_loan_fe as house_loan_fe, dc.age_loan_fe as age_loan_fe, ")
				.append("dc.houseAge as houseAge, dc.createdate as createdate, dc.updatedate as updatedate, dc.isdelete as isdelete\r\n")
				.append("FROM loan_bankmeta dc\r\n")
				.append("where 1=1");
		if(!StringUtils.isEmpty(bankMeta.getBankName())) {
			values.put("bankName", "%" + bankMeta.getBankName() + "%");
			queryString.append(" and dc.bankName like :bankName\r\n");
		}
		StringBuffer countString = new StringBuffer("select count(1) from (").append(queryString).append(") a");
		return bankMetaDao.sqlQuery(queryString.toString(), countString.toString(), values, pageable);
	}
	
	@Override
	public void deleteBank(Long deletebankid) {
		bankMetaDao.deleteById(deletebankid);
	}
}
